package com.wwwarehouse.taskcenter.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.InviteSuccessBean;

import java.util.List;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/11 13:05
 * @描述 ${TODO}
 */

public class InviteSuccessAdapter extends BaseAdapter {

    private Context                          mContext;
    private List<InviteSuccessBean.ListBean> inviteSuccessChildList;
    private ListView                              mListView;

    public InviteSuccessAdapter(Context context, List<InviteSuccessBean.ListBean> list, ListView listview) {
        mContext = context;
        inviteSuccessChildList = list;
        mListView = listview;
    }

    @Override
    public int getCount() {
        return inviteSuccessChildList == null ? 0 : inviteSuccessChildList.size();
    }

    @Override
    public Object getItem(int position) {
        return inviteSuccessChildList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    ViewHolder viewHolder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        InviteSuccessBean.ListBean listBean = inviteSuccessChildList.get(position);

        if (convertView == null) {
            convertView = parent.inflate(mContext, R.layout.invite_success_item, null);
            viewHolder = new ViewHolder();
            viewHolder.civGood = (CircleImageView) convertView.findViewById(R.id.civ_good);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tvData = (TextView) convertView.findViewById(R.id.tv_data);
            viewHolder.mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.rl);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (InviteSuccessAdapter.ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                int itemHeight = (mListView.getMeasuredHeight() - mListView.getPaddingTop() - mListView.getPaddingBottom()) / 8;
                params.height = itemHeight;
                finalConvertView.setLayoutParams(params);
                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        BaseApplication.getApplicationInstance().displayImg(listBean.getFaceUrl(), viewHolder.civGood);
        viewHolder.tvName.setText(listBean.getInviteeName());
        viewHolder.tvData.setText(listBean.getAuditDate());
        return convertView;
    }

    static class ViewHolder {
        private CircleImageView civGood;
        private TextView        tvName;
        private TextView        tvData;
        private RelativeLayout  mRelativeLayout;
    }
}
